﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Axis.Dao.Research;
using PhuLeBooze.Global;
using Axis.BusinessObject.Research;
using PhuLeBooze.Views.Project;
using System.Web.Script.Serialization;
using System.Text;
using Axis.BusinessObject.Core;

namespace PhuLeBooze.Views.Research
{
    public class CQuestionUI : System.Web.UI.Page
    {
        public string GetXmlRequest(HttpContext context)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");

            XmlElement InputValue = doc.CreateElement("InputValue");
            HttpRequest request = context.Request;
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            if (request["ProjectID"] != null)
                InputValue.SetAttribute("ProjectID", request["ProjectID"].Trim());
            if (request["ID"] != null)
                InputValue.SetAttribute("ID", request["ID"].Trim());
            if (request["Type"] != null)
                InputValue.SetAttribute("Type", request["Type"].Trim());
            if (request["QuestionText"] != null)
                InputValue.SetAttribute("QuestionText", request["QuestionText"].Trim());
            if (request["Action"] != null)
                InputValue.SetAttribute("Action", request["Action"].Trim());
            if (request["ZOrder"] != null)
                InputValue.SetAttribute("ZOrder", request["ZOrder"].Trim());
            if (request["ScaleFromText"] != null)
                InputValue.SetAttribute("ScaleFromText", request["ScaleFromText"].Trim());
            if (request["ScaleToText"] != null)
                InputValue.SetAttribute("ScaleToText", request["ScaleToText"].Trim());
            if (request["ScaleFrom"] != null)
                InputValue.SetAttribute("ScaleFrom", request["ScaleFrom"].Trim());
            if (request["ScaleTo"] != null)
                InputValue.SetAttribute("ScaleTo", request["ScaleTo"].Trim());
            if (request["Code"] != null)
                InputValue.SetAttribute("Code", request["Code"].Trim());
            // if (request["DisplayRandomResponse"] != null)
            InputValue.SetAttribute("DisplayRandomResponse", "0");
            InputValue.SetAttribute("DependentType", "0");
            root.AppendChild(InputValue);
            if (request["ResponseOption"] != null)
            {
                CResponseUI responseUI = new CResponseUI();
                string temp = HttpUtility.HtmlDecode(request["ResponseOption"].Trim());
                IList<CResponseOption> List = responseUI.GetResponseList(temp);
                foreach (CResponseOption row in List)
                {
                    XmlElement ele = doc.CreateElement("ResponseOption");
                    ele.SetAttribute("ID", row.ID.ToString());
                    ele.SetAttribute("Caption", row.Caption.ToString());
                    ele.SetAttribute("Value", row.Value.ToString());
                    ele.SetAttribute("Description", row.Description.ToString());
                    ele.SetAttribute("ZOrder", row.ZOrder.ToString());
                    ele.SetAttribute("AllowInputText", "0");
                    root.AppendChild(ele);
                }
                IList<CResponseOption> ListCol = responseUI.GetResponseListCol(temp);
                foreach (CResponseOption row in ListCol)
                {
                    XmlElement ele = doc.CreateElement("SubQuestionnaire");
                    ele.SetAttribute("ID", row.ID.ToString());
                    ele.SetAttribute("QuestionText", row.Caption.ToString());
                    ele.SetAttribute("Value", row.Value.ToString());
                    ele.SetAttribute("Description", row.Description.ToString());
                    ele.SetAttribute("ZOrder", row.ZOrder.ToString());
                    ele.SetAttribute("AllowInputText", "0");
                    root.AppendChild(ele);
                }
            }
            return root.InnerXml;
        }
        public IList<CQuestionaire> GetQuestionareList(int QuestionnaireID, int ProjectID)
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            CResearchDao core = new CResearchDao();
            return core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ID='{1}' ProjectID='{2}'/>", member.ID, QuestionnaireID, ProjectID));
        }
        public IList<CQuestionaire> GetQuestionareList(int ProjectID)
        {
            CResearchDao core = new CResearchDao();
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            return core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ProjectID='{1}'/>", member.ID, ProjectID));
        }

        public string GetQuestionareList(int QuestionnaireID, string ProjectID, out int QuesKey)
        {
            CResearchDao core = new CResearchDao();
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            QuesKey = 0;
            IList<CQuestionaire> Map = core.GetQuestionaire(string.Format("<InputValue UserID='{0}' ID='{1}' ProjectID='{2}'/>", member.ID.ToString(), QuestionnaireID, ProjectID));
            if (Map.Count > 0)
                QuesKey = Map[0].ID;
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(Map);
        }

        public string GetResponseListByQuestionnaire(int QuestionnaireID)
        {
            CResearchDao core = new CResearchDao();
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            IList<CResponseOption> Map = core.GetResponseOption(string.Format("<InputValue UserID=\"{0}\" QuestionnaireID=\"{1}\" />", member.ID.ToString(), QuestionnaireID));
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(Map);
        }

        public string GetQuestionDetail(string ProjectID)
        {
            StringBuilder ret = new StringBuilder();
            StringBuilder result = new StringBuilder();
            CResearchDao cResearchDao = new CResearchDao();
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("Action", "GET");
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("RowsPerPage", "999");
            root.SetAttribute("ProjectID", ProjectID);
            IList<CQuestionaire> list = cResearchDao.GetQuestionaire(root.OuterXml);
            string QuesRow = "";
            string QuesCol = "";
            int QuesKey = 0;
            if (list.Count > 0)
            {
                int QuesID = list[0].ID;
                QuesRow = GetQuestionareList(QuesID, ProjectID, out QuesKey);
                QuesCol = GetResponseListByQuestionnaire(QuesKey);

            }
            result.AppendFormat("{{\"QuesKey\":{0},", QuesKey);
            if (QuesRow.Length > 0)
                result.AppendFormat("\"QuesRow\":{0},", QuesRow);
            else
                result.Append("\"QuesRow\":[],");
            if (QuesCol.Length > 0)
                result.AppendFormat("\"QuesCol\":{0}", QuesCol);
            else
                result.Append("\"QuesCol\":[]");
            result.Append("}\n");
            return result.ToString();
        }
    }
}